package ee.maoinvest.backend.services.implementations;

import java.sql.Connection;

import org.apache.log4j.Logger;

import ee.maoinvest.backend.database.ConnectionManager;
import ee.maoinvest.backend.model.dao.ElectricityMeterReadingDAO;
import ee.maoinvest.backend.model.dao.factory.DAOFactory;
import ee.maoinvest.backend.model.data.ReadingType;
import ee.maoinvest.backend.services.ElectricityMeterReadingsService;

public class ElectricityMeterReadingsServiceImpl implements ElectricityMeterReadingsService
{
	Logger logger = Logger.getLogger(ElectricityMeterReadingsServiceImpl.class);
	
	int OPERATION_WAS_SUCCESSFUL = 1;
	int OPERATION_WAS_UNSUCCESSFUL = 0;
	//DAOFactory must definitely be  before ConnectionManager, because it initializes ConnectionManager
	//and can potentially do it here
	DAOFactory daoFactory = DAOFactory.getDAOFactory();
	
	@Override
	public ReadingType getTypeOfTheMostRecentElectricityMeterReading()
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		ElectricityMeterReadingDAO electricityMeterReadingDAOImpl = daoFactory.getElectricityMeterReadingDAOImpl(dbConnection);
		
		ReadingType readingTypeRetrieved = null;
		
		try 
		{
			readingTypeRetrieved = electricityMeterReadingDAOImpl.getTypeOfTheMostRecentElectricityMeterReading();
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeConnection();
		}
		
		return readingTypeRetrieved;
	}
}
